package com.zxq.ruoyi.channel.device;



import com.zxq.frame.core.ExchangeDTO;
import com.zxq.ruoyi.channel.SynchroniseMessage;
import com.zxq.ruoyi.channel.translator.impl.TransformServiceImpl;
import com.zxq.ruoyi.domain.vo.DevCmdVo;
import com.zxq.ruoyi.httpclient.HttpUtils;
import com.zxq.ruoyi.service.impl.IotCmdServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;

@Service
@Slf4j
public class DeviceMessageServiceImpl implements IDeviceMessage {

    /**
     * http客户端操作，请求
     */
    @Autowired
    HttpUtils httpUtils;

    /**
     * 翻译的操作
     */
    @Autowired
    TransformServiceImpl transformService;

    /**
     * 同步消息对象
     */
    @Autowired
    SynchroniseMessage synchroniseMessage;

    /**
     *指令服务
     * */
    @Autowired
    IotCmdServiceImpl iotCmdService;




    static DeviceMessageServiceImpl instance = null ;

    public static DeviceMessageServiceImpl getInstance() {
        return instance;
    }


    @PostConstruct
    public void init() {
        instance = this;
    }



    @Override
    public void deviceReport(ExchangeDTO exchangeDTO, String reportUrl, String token) {
        DevCmdVo devCmdVo = transformService.devTransform(exchangeDTO);
        httpUtils.doReport(devCmdVo,reportUrl,token);
    }

    @Override
    public void deviceResult(ExchangeDTO exchangeDTO) {
        DevCmdVo devCmdVo = transformService.devTransform(exchangeDTO);
        Long resultId = iotCmdService.selectIotCmdById(devCmdVo.getCmdId()).getId();
        synchroniseMessage.notifyPlatformMessage(devCmdVo.getDevId(),resultId,devCmdVo);
    }


}
